var Message =require('../../common/Message');
let Queue = {
  list: [],
  duration: 1800000, //过期时间:30分钟
  match: function (phone, code) {
    //验证码比对
    let arr = this.list;
    for (let sms of arr) {
      if (sms.phone == phone && sms.smsCode == code) {
        return true;
      }
    }
    return false;
  },
  testing: function () {
    console.log('短信验证超时检测启动........');
    setInterval(() =>{
      //每隔5秒对 LIST 轮询一遍检测验证码是否过期,过期则删除
      let now = Date.now();
      let list = this.list;
      list.forEach(function (sms, index) {
        console.log('检测:' + sms.phone + ' 剩余数量:' + list.length);
        if (now - sms.time > this.duration) {
          remove(sms.phone);
        }
      })
    }, 5000)
  },
  remove:function(phone){
    let arr = this.list;
    for (let i=0;i<arr.length;i++) {
      let sms = arr[i];
      if (sms.phone == phone) {
        this.list.splice(i,1);
      }
    }
  }
}

//启动检测
Queue.testing();

module.exports = Queue;


//测试
//Queue.list.push(new Message(18682920673,7891,Date.now()));